﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace BDSim
{
    /// <summary>
    /// 用户段
    /// </summary>
    public class UserSegment : SegmentBase
    {
        ObservableCollection<UserBaseModel> _UserModelList = new ObservableCollection<UserBaseModel>();

        [Browsable(false)]
        public ObservableCollection<UserBaseModel> UserModelList
        {
            get { return _UserModelList; }
            set { _UserModelList = value; NotifyPropertyChanged("UserModelList"); }
        }

        public UserSegment()
        {
            Name = "用户段";
            DefineCmd();
            Menu.ItemsSource = MenuCommandList;
        }

        #region 字段
        int StaticUserNum = 1;
        int AircraftNum = 1;
        int CarNum = 1;
        int BoatNum = 1;
        //int MissileUserNum = 1;
        //int SpaceUserNum = 1;

        #endregion

        #region 初始化
        public override void Init()
        {
            base.Init();
            foreach (var item in UserModelList)
            {
                item.Init();
            }
        }
        #endregion

        #region 单步运行
        public override void StepRun()
        {
            base.StepRun();
            foreach (var item in UserModelList)
            {
                item.StepRun();
            }
        }
        #endregion

        #region 右键命令
        List<MenuCommand> MenuCommandList = new List<MenuCommand>();

        MenuCommand AddUser = new MenuCommand();
        MenuCommand AddStaticUser = new MenuCommand();
        MenuCommand AddAirUser = new MenuCommand();
        MenuCommand AddCarUser = new MenuCommand();
        MenuCommand AddBoatUser = new MenuCommand();
        MenuCommand AddMilseUser = new MenuCommand();
        MenuCommand AddSpaceUser = new MenuCommand();
        MenuCommand ClearUserSegment = new MenuCommand();

        void DefineCmd()
        {
            AddUser.Header = "新建用户";
            MenuCommandList.Add(AddUser);

            AddStaticUser.Header = "静态用户";
            AddStaticUser.DefineExcuteCommand(AddStaticUserFun_Execute, AddStaticUserFun_CanExecute);
            AddUser.SubMenuCommandList.Add(AddStaticUser);
            
            AddAirUser.Header = "飞机用户";
            AddAirUser.DefineExcuteCommand(AddAirUserFun_Execute, AddAirUserFun_CanExecute);
            AddUser.SubMenuCommandList.Add(AddAirUser);

            AddCarUser.Header = "汽车用户";
            AddCarUser.DefineExcuteCommand(AddCarUserFun_Execute, AddCarUserFun_CanExecute);
            AddUser.SubMenuCommandList.Add(AddCarUser);

            AddBoatUser.Header = "船用户";
            AddBoatUser.DefineExcuteCommand(AddBoatUserFun_Execute, AddBoatUserFun_CanExecute);
            AddUser.SubMenuCommandList.Add(AddBoatUser);

            //AddMilseUser.Header = "弹道用户";
            //AddMilseUser.DefineExcuteCommand(AddTrajectoryUserFun_Execute, AddTrajectoryUserFun_CanExecute);
            //AddUser.SubMenuCommandList.Add(AddMilseUser);

            //AddSpaceUser.Header = "航天器用户";
            //AddSpaceUser.DefineExcuteCommand(AddSpaceUserFun_Execute, AddSpaceUserFun_CanExecute);
            //AddUser.SubMenuCommandList.Add(AddSpaceUser);

            ClearUserSegment.Header = "清空";
            ClearUserSegment.DefineExcuteCommand(ClearUserSegmentFun_Execute, ClearUserSegmentFun_CanExecute);
            MenuCommandList.Add(ClearUserSegment);
        }

        void AddStaticUserFun_Execute(object param)
        {
            StaticUser staticUser = new StaticUser() {Parent = this };
            staticUser.Name = "静态用户" + StaticUserNum;
            //staticUser.Id = UserModelList.Count() + 1;

            CReceiver rec = new CReceiver();
            rec.Parent = staticUser;
            rec.Name = "Receiver1";
            rec.Id = 1;
            staticUser.RecCollection.Add(rec);//

            UserModelList.Add(staticUser);
            StaticUserNum++;
        }
        bool AddStaticUserFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        void AddCarUserFun_Execute(object param)
        {
            Car car = new Car() { Parent = this };
            car.Name = "汽车用户" + CarNum;

            CReceiver rec = new CReceiver();
            rec.Parent = car;
            rec.Name = "Receiver1";
            rec.Id = 1;
            car.RecCollection.Add(rec);//

            UserModelList.Add(car);
            CarNum++;
        }
        bool AddCarUserFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        void AddAirUserFun_Execute(object param)
        {
            Aircraft airuser = new Aircraft() { Parent = this };
            airuser.Name = "飞机用户"+AircraftNum;

            CReceiver rec = new CReceiver();
            rec.Parent = airuser;
            rec.Name = "Receiver1";
            rec.Id = 1;
            airuser.RecCollection.Add(rec);//

            UserModelList.Add(airuser);
            AircraftNum++;
        }
        bool AddAirUserFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        void AddBoatUserFun_Execute(object param)
        {
            Ship shipuser = new Ship() { Parent = this };
            shipuser.Name = "船用户" + BoatNum;

            CReceiver rec = new CReceiver();
            rec.Parent = shipuser;
            rec.Name = "Receiver1";
            rec.Id = 1;
            shipuser.RecCollection.Add(rec);//

            UserModelList.Add(shipuser);
            BoatNum++;
        }
        bool AddBoatUserFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }

        //void AddTrajectoryUserFun_Execute(object param)
        //{
        //    Missile trajectory = new Missile() { Parent = this };
        //    trajectory.Name = "弹道用户" + MissileUserNum;

        //    CReceiver rec = new CReceiver();
        //    rec.Parent = trajectory;
        //    rec.Name = "Receiver1";
        //    rec.Id = 1;
        //    trajectory.RecCollection.Add(rec);//

        //    UserModelList.Add(trajectory);
        //    MissileUserNum++;
        //}
        //bool AddTrajectoryUserFun_CanExecute(object param)
        //{
        //    bool flag = false;
        //    if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
        //    {
        //        flag = true;
        //    }
        //    return flag;
        //}

        //void AddSpaceUserFun_Execute(object param)
        //{
        //    SpaceVehicle spaceuser = new SpaceVehicle() { Parent=this};
        //    spaceuser.Name = "航天器用户"+SpaceUserNum;

        //    CReceiver rec = new CReceiver();
        //    rec.Parent = spaceuser;
        //    rec.Name = "Receiver1";
        //    rec.Id = 1;
        //    spaceuser.RecCollection.Add(rec);//

        //    UserModelList.Add(spaceuser);
        //    SpaceUserNum++;
        //}
        //bool AddSpaceUserFun_CanExecute(object param)
        //{
        //    bool flag = false;
        //    if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
        //    {
        //        flag = true;
        //    }
        //    return flag;
        //}

        void ClearUserSegmentFun_Execute(object param)
        {
            UserModelList.Clear();
        }
        bool ClearUserSegmentFun_CanExecute(object param)
        {
            bool flag = false;
            if (BDSimObject.GlobalStatus == ESimStatus.UnInited || BDSimObject.GlobalStatus == ESimStatus.End)
            {
                flag = true;
            }
            return flag;
        }
        

        #endregion 右键命令

        internal void AddBaseUser(UserBaseModel userBaseModel)
        {
            userBaseModel.Parent = this;
            UserModelList.Add(userBaseModel);
        }
    }
}
